package com.sumainfor.pc.market.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.OwnerMapper;
import com.sumainfor.pc.market.dao.ProfitMapper;
import com.sumainfor.pc.market.dao.SePactMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import com.sumainfor.pc.market.feign.Zfeign.ZzuulFeign;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务中心经理审核签约的合同
 */
@Service
public class SePactService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(CommunityService.class);
    String UUID=null;
    @Autowired
    private UtilsMapper utilsMapper;
    @Autowired
    private SePactMapper sePactMapper;
    @Autowired
    private ZzuulFeign zzuulFeign;
    /**
     * 服务中心经理查询待审核合同列表
     */
    public  List<Map<String,Object>> getSePactList(Map<String, Object> params){
        List<Map<String,Object>> marketList=sePactMapper.getSePactList(params);
        List<String> sysUserList=new ArrayList<String>();
        for (Map<String,Object> map: marketList){
            if(map.get("SysUserId")!=null){
                sysUserList.add(map.get("SysUserId").toString());
            }
        }
        params.put("sysUserList",sysUserList);
        System.out.println("传递到网关的参数:"+sysUserList);
        //查询合同申请人信息
        JsonResult zuulResut=zzuulFeign.getReapirSysUser(params);
        if (zuulResut.get("errorCode").toString().equals("0")){
            List<Map<String,Object>> zuulList=(List<Map<String,Object>>)zuulResut.get("result");
            for (Map<String,Object> map1:zuulList){
                if (map1.get("SysUserID")!=null){
                    for (Map<String,Object> map2:marketList){
                        if (map2.get("SysUserId")!=null){
                            if (map1.get("SysUserID").toString().equals(map2.get("SysUserId").toString())){
                                map2.putAll(map1);
                            }
                        }
                    }
                }
            }
        }
        return marketList;
    }
    /**
     * 查询待审核的合同列表总数
     */
    public Integer getSePactListCount(Map<String,Object> params){
        return sePactMapper.getSePactListCount(params);

    }
    /**
     * 服务中心租赁顾问管理二级页面查询本月签单客户
     */
    public JsonResult getSePactTenList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("SysUserId")==null){
            return result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=sePactMapper.getSePactTenList(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
    /**
     * 查询内部房源在第三方房源信息
     */
    public JsonResult getSePactThird(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("HouInfoId")==null && params.get("HouId")==null){
            return result.putFailed("服务器繁忙");
        }
        Map<String,Object> map=sePactMapper.getSePactThird(params);
        if (map!=null){
            result.put(map);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
}
